﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Diagnostics;

namespace MovieTweet
{
	public partial class GlavnaForma : Form
	{
		DataTable dtInTheaters = new DataTable();
		DataTable dtUpcoming = new DataTable();
		DataTable dtDVD = new DataTable();

		private bool countFlag = true;

		System.Windows.Forms.Timer timerTwitter = new System.Windows.Forms.Timer();
		System.Windows.Forms.Timer timerRotten = new System.Windows.Forms.Timer();
		
		public GlavnaForma()
		{
			InitializeComponent();

			dtInTheaters.Columns.Add("ID");
			dtInTheaters.Columns.Add("Name");
			dtInTheaters.Columns.Add("Mentions");
			dtUpcoming.Columns.Add("ID");
			dtUpcoming.Columns.Add("Name");
			dtUpcoming.Columns.Add("Mentions");
			dtDVD.Columns.Add("ID");
			dtDVD.Columns.Add("Name");
			dtDVD.Columns.Add("Mentions");
			dtInTheaters.Columns[2].DataType = System.Type.GetType("System.Int32");
			dtUpcoming.Columns[2].DataType = System.Type.GetType("System.Int32");
			dtDVD.Columns[2].DataType = System.Type.GetType("System.Int32");

			
			//dgvMovies.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
			//dgvMovies.Columns[2].DefaultCellStyle.WrapMode = DataGridViewTriState.True;


			timerTwitter.Interval = 500;
			timerTwitter.Tick += new EventHandler(timerTwitter_Tick);
			timerRotten.Interval = 500;
			timerRotten.Tick += new EventHandler(timerRotten_Tick);

			ListOfMovieMentions.list = new List<MovieInfo>();
			ListOfMovieMentions.loaded = false;

			RottenMovieInfo.dtInTheaters = new DataTable();
			RottenMovieInfo.dtUpcoming = new DataTable();
			RottenMovieInfo.dtDVD = new DataTable();
			RottenMovieInfo.dtInTheaters.Columns.Add("ID");
			RottenMovieInfo.dtInTheaters.Columns.Add("Name");
			RottenMovieInfo.dtInTheaters.Columns.Add("Mentions");
			RottenMovieInfo.dtUpcoming.Columns.Add("ID");
			RottenMovieInfo.dtUpcoming.Columns.Add("Name");
			RottenMovieInfo.dtUpcoming.Columns.Add("Mentions");
			RottenMovieInfo.dtDVD.Columns.Add("ID");
			RottenMovieInfo.dtDVD.Columns.Add("Name");
			RottenMovieInfo.dtDVD.Columns.Add("Mentions");
			RottenMovieInfo.dtInTheaters.Columns[2].DataType = System.Type.GetType("System.Int32");
			RottenMovieInfo.dtUpcoming.Columns[2].DataType = System.Type.GetType("System.Int32");
			RottenMovieInfo.dtDVD.Columns[2].DataType = System.Type.GetType("System.Int32");
			RottenMovieInfo.loaded = false;
		}

		void timerRotten_Tick(object sender, EventArgs e)
		{
			if (RottenMovieInfo.loaded)
			{
				this.dtInTheaters = RottenMovieInfo.dtInTheaters;
				this.dtUpcoming = RottenMovieInfo.dtUpcoming;
				this.dtDVD = RottenMovieInfo.dtDVD;
				
				setMovieSource();
				
				timerRotten.Stop();
                btnRefresh.Enabled = true;

			}
		}

		void timerTwitter_Tick(object sender, EventArgs e)
		{
			foreach (MovieInfo info in ListOfMovieMentions.list)
			{
				foreach (DataGridViewRow row in dgvMovies.Rows)
				{
					if (String.Equals(row.Cells[1].Value.ToString(), info.movieTitle))
					{
						row.Cells[2].Value = info.mentions;
					}
				}
			}

                if (ListOfMovieMentions.loaded)
                {
                    dgvMovies.Sort(dgvMovies.Columns[2], ListSortDirection.Descending);
                    timerTwitter.Stop();
                }

		}

		private void btnSettings_Click(object sender, EventArgs e)
		{
			(new FrmSettings()).ShowDialog();
		}

		private void btnDetails_Click(object sender, EventArgs e)
		{
			string movieId = dgvMovies.SelectedRows[0].Cells[0].Value.ToString();
			(new FrmMovieDetails(movieId)).Show();
		}

		private void GlavnaForma_Load(object sender, EventArgs e)
		{
			Thread workerRotten = new Thread(getRottenInfo);
			workerRotten.Start();
			timerRotten.Start();
			
			cboSource.SelectedIndex = 1;
			foreach (DataGridViewRow row in dgvMovies.Rows)
			{
				MovieInfo men = new MovieInfo();
				men.movieTitle = row.Cells[1].Value.ToString();
				men.mentions = 0;
				ListOfMovieMentions.list.Add(men);
			}
		}

		private void getRottenInfo()
		{
            try
            {
                RottenInfo info = new RottenInfo("In theaters");
                foreach (RottenMovieID result in info.getList())
                {
                    object[] objectResult = new object[] { result.id, result.name, null };
                    RottenMovieInfo.dtInTheaters.Rows.Add(objectResult);
                }

                info = new RottenInfo("Upcoming");
                foreach (RottenMovieID result in info.getList())
                {
                    object[] objectResult = new object[] { result.id, result.name, null };
                    RottenMovieInfo.dtUpcoming.Rows.Add(objectResult);
                }

                info = new RottenInfo("DVD");
                foreach (RottenMovieID result in info.getList())
                {
                    object[] objectResult = new object[] { result.id, result.name, null };
                    RottenMovieInfo.dtDVD.Rows.Add(objectResult);
                }
                RottenMovieInfo.loaded = true;
            }
            catch (Exception e) { MessageBox.Show(e.Message);
            }
		}

		private void cboSource_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (RottenMovieInfo.loaded)
				setMovieSource();
		}

		private void setMovieSource()
		{
			switch (cboSource.SelectedItem.ToString())
			{
				case ("In theaters"):
					dgvMovies.DataSource = this.dtInTheaters;
					break;

				case ("Upcoming"):
					dgvMovies.DataSource = this.dtUpcoming;
					break;

				case ("DVD"):
					dgvMovies.DataSource = this.dtDVD;
					break;

				default: break;
			}

			
			dgvMovies.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            

			lblCount.Text = "Movies found: " + dgvMovies.Rows.Count.ToString();
		}

		private void countMentionsForAll()
		{
            try
            {
                for (int i = 0; i < ListOfMovieMentions.list.Count; i++)
                {
                    if (countFlag == false) break;
                    TwitterInfo info = new TwitterInfo();
                    ListOfMovieMentions.list[i].mentions = info.countMentions(ListOfMovieMentions.list[i].movieTitle);
                }
                ListOfMovieMentions.loaded = true;
            }
            catch (Exception e) { MessageBox.Show(e.Message); }
		}

		private void btnRefresh_Click(object sender, EventArgs e)
		{
			ListOfMovieMentions.list = new List<MovieInfo>();
			ListOfMovieMentions.loaded = false;
			countFlag = true;

			foreach (DataGridViewRow row in dgvMovies.Rows)
			{
				MovieInfo men = new MovieInfo();
				men.movieTitle = row.Cells[1].Value.ToString();
				men.mentions = 0;
				ListOfMovieMentions.list.Add(men);
			}
			
			Thread worker = new Thread(countMentionsForAll);
			worker.Start();
			timerTwitter.Start();
		}

		private void btnStop_Click(object sender, EventArgs e)
		{
			countFlag = false;
		}

		//private void DoubleClickEvent(object sender, DataGridViewCellEventArgs e)
		//{
		//    //string movieId = dgvMovies.SelectedRows[0].Cells[0].Value.ToString();
		//    //(new FrmMovieDetails(movieId)).Show();
		//}
	}

	public class MovieInfo
	{
		public string movieTitle;
		public int mentions;
		public string score;
	}

	public static class ListOfMovieMentions
	{
		public static List<MovieInfo> list;
		public static bool loaded;
	}

	public static class RottenMovieInfo
	{
		public static DataTable dtInTheaters;
		public static DataTable dtUpcoming;
		public static DataTable dtDVD;
		public static bool loaded;
	}

	
}
